<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <!--
        el-table 属性
          data: 表格数据, 必须是一个数组
        el-table-column
          width: 宽度
          label: 表头的文字
          prop: 字符串类型, 要在此列渲染的数据属性名
       -->
      <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题"
        width="350">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者"
        width="130">
      </el-table-column>
      <el-table-column
        prop="likeCount"
        label="点赞"
        width="130">
      </el-table-column>
      <el-table-column
        prop="views"
        label="浏览数"
        width="130">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="更新时间"
        width="200">
      </el-table-column>
      <el-table-column
        label="操作">
        <!-- 作用域插槽: 获取组件内部的数据在父组件中显示或者操作 -->
        <template #default="{ row }">
          <div class="actions">
            <i class="el-icon-view" @click="openDrawer('preview', row.id)"></i>
            <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
            <i class="el-icon-delete" @click="del(row.id)"></i>
          </div>
        </template>
      </el-table-column>
      </el-table>
      <!-- <el-pagination
      @size-change="handleSizeChange"                   每页显示的条数改变时触发
      @current-change="handleCurrentChange"             当前的页码值改变时触发
      :current-page="currentPage4"                      控制当前页数
      :page-sizes="[100, 200, 300, 400]"                每页显示多少条数据的选项
      :page-size="100"                                  控制每页显示的条数
      layout="total, sizes, prev, pager, next, jumper"  用来给分页器布局的
      :total="400">                                     显示总的数据条数
    </el-pagination> -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="q.current"
      :page-sizes="[10, 15, 20, 30]"
      :page-size="q.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>

    <!-- 放抽屉组件 -->
    <el-drawer
      size="50%"
      :title="drawerTitle"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose">
      <el-form v-if="drawerType !== 'preview'" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
          v-model="form.content"
          @blur="$refs.form.validataField('content')" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
      <div v-else class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getArticles, addArticles, getDelete, getArticlesDetail, updateArticle } from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  data () {
    return {
      currentPage4: 4,
      current: 1,
      pageSize: 10,
      // 收集表单的数据对象
      form: {
        stem: '',
        content: ''
      },
      // 验证规则对象
      rules: {
        stem: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      q: {
        current: 1,
        pageSize: 10
      },
      total: 0,
      list: [],
      drawer: false,
      drawerType: '' // 保存的时候组件操作的类型 添加 || 预览 || 修改
    }
  },
  computed: {
    drawerTitle () {
      let title = '添加面经'
      if (this.drawerType === 'add') {
        title = '添加面经'
      } else if (this.drawerType === 'preview') {
        title = '面经预览'
      } else {
        title = '修改面经'
      }
      return title
    }
  },
  created () {
    this.loadArticle()
  },
  methods: {
    // 获取列表的数据进行渲染
    async loadArticle () {
      const res = await getArticles(this.q)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      // console.log(this.list)
    },
    // 当每页显示的条数进行切换时触发  val: 获取当前每页显示的条数
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.q.current = 1
      // 改变查询 q 对象中的 pageSize 的值  pageSize --> val
      this.q.pageSize = val
      // 重新发送请求获取数据, 渲染页面
      this.loadArticle()
    },
    // 当前的页码值进行切换时触发  val: 当前的页码值
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      // 改变查询 q 对象中的 current 的值  current --> val
      this.q.current = val
      // 重新发送请求获取数据, 渲染页面
      this.loadArticle()
    },
    // 区别不同的操作, 打开抽屉组件
    async openDrawer (type, id) {
      console.log(type, id)
      this.drawerType = type // 保存操作的类型
      // 只要不是新增 其他 预览或者新增 都需要获取到当前 id 对相应的数据填充到表单
      if (type !== 'add') {
        // 发送请求 将数据填充到表单
        const res = await getArticlesDetail(id)
        console.log(res, 'getArticlesDetail')
        this.form = res.data
      }
      // 打开抽屉组件
      this.drawer = true
    },
    handleClose () {
      // 重置表单 --> 重置表单的校验规则, 重置表单的数据
      // 需要注意 --> resetFields 只能重置 el-form-item 中带有 prop="stem" 属性的表单项
      if (this.drawerType !== 'preview') {
        this.$refs.form.resetFields()
      }
      // 手动重置数据
      this.form = {
        stem: '',
        current: ''
      }
      // 关闭抽屉组件
      this.drawer = false
    },
    async submit () {
      // await this.$refs.form.validate() 返回值是一个promise实例
      try {
        // 1. 兜底校验
        await this.$refs.form.validate()
        // 2. 发送请求
        let res = null
        // 新增面经
        if (this.drawerType === 'add') {
          res = await addArticles(this.form)
          console.log(res, 'addArticles')
        } else {
          // 编辑面经
          const { id, stem, content } = this.form
          res = await updateArticle({ id, stem, content })
          console.log(res, 'updateArticle')
        }
        // 3. 请求 成功 | 失败 提示用户
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 手动将页码值改成 1
        this.q.current = 1
        // 4. 重新渲染页面
        this.loadArticle()
        // 5. 关闭抽屉组件
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    },
    async del (id) {
      // console.log(id)
      // 发送请求
      const res = await getDelete(id)
      // console.log(res)
      // 提示用户
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 3. 重新获取数据
      this.loadArticle()
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
